import {
  exitAlumnia
} from '../../../services/alumina/index'
import { getMyAssociationList } from '../../../services/user/index'
import Toast from 'tdesign-miniprogram/toast';
Page({
  data: {
    list: [],
    openId: wx.getStorageSync('openid')
  },
  async init() {
    try {
      const result = await getMyAssociationList(this.data.openId);
      if (result.data.code == 200) {
        this.setData({
          list: result.data.rows || []
        });
      }
    } catch (error) {
      console.error('err:', error);
    }
  },

  onShow() {
    const openId = wx.getStorageSync('openid');
    if (this.data.openId !== openId) {
      this.setData({
        openId
      })
      this.init(true)
    }
  },
  onLoad() {
    this.init(true);
  },
  exitHandler(e) {
    const { associationid } = e.target.dataset
    const { openId } = this.data;
    if (!openId) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请先登录',
      });
      wx.navigateTo({
        url: '/pages/usercenter/index'
      })
    }
    exitAlumnia({
      openId: openId,
      associationId: associationid
    }).then(res => {
      console.log(res.data)
      if (res.data.code == 200) {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '退出成功',
        });
        this.init()
      } else {
        Toast({
          context: this,
          selector: '#t-toast',
          message: res.data.msg,
        });
      }
    })
   
  },
  goDetail(event) {
    const target = event.target;
    // 如果没有获取到，也可以使用 event.currentTarget 获取绑定事件的源素
    const current = event.currentTarget;
    console.log(target, current)
    let a = target.dataset.a;
    if (!a) {
      a = current.dataset.a
    }
    wx.navigateTo({
      url: `/pages/alumniaDetail/index?noticeId=${a}`,
    });
  }
});